package com.woniuxy.rms.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.rms.advice.LogPoint;
import com.woniuxy.rms.config.UploadConfig;
import com.woniuxy.rms.pojo.Carousels;
import com.woniuxy.rms.service.CarouselService;
import com.woniuxy.rms.util.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import java.util.*;
import java.util.stream.Collectors;
/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/31 0031:下午 9:21  <br/>
 * Description:轮播控制层
 */
@Controller
@RequestMapping("/carousel")
@Api("轮播图管理")
public class CarouselController {

    @Autowired
    CarouselService carouselService;

    @Value("${upfile.path}")
    String loadPath;

    @Value("${upfile.imgPath}")
    String imgPath;

    @Autowired
    UploadConfig uploadConfig;

    /**
     * 删除多条轮播数据
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDel")
    @ResponseBody
    @LogPoint("批量删除轮播图")
    @RequiresPermissions("carousel:delete")
    @ApiOperation(value = "批量删除轮播图")
    public DataResult batchDel(@ApiParam(name = "ids",value = "需要删除的id字符串",required = true) String ids){
        //转换id数组
        List<Long> id = Arrays.stream(ids.split(",")).map(s -> Long.parseLong(s)).collect(Collectors.toList());
        try {
            carouselService.batchDelById(id);
            return DataResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.failed();
        }
    }
    /**
     * 删除一条轮播图数据
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    @ResponseBody
    @LogPoint("删除一条轮播图")
    @RequiresPermissions("carousel:delete")
    @ApiOperation("删除一条轮播图")
    public DataResult del(@ApiParam(name = "id",required = true ,value = "轮播图id") Byte id){
        try {
            carouselService.delById(id);
            return DataResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.failed();
        }
    }
    /**
     * 更改轮播图状态
     * @param cid
     * @param status
     * @return
     */
    @PutMapping("/updateStatus")
    @ResponseBody
    @LogPoint("更改轮播图状态")
    @RequiresPermissions("carousel:update")
    @ApiOperation("更改轮播图状态（是否展示）")
    public DataResult updateStatus(@ApiParam(name = "cid",value = "轮播图id",required = true) Byte cid,
                                   @ApiParam(name = "status",value = "状态码",required = true) Integer status){
        try {
            carouselService.updateStatusById(cid,status);
            return DataResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.failed();
        }
    }
    /**
     * 跳转到编辑页面
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editCarousel/{id}")
    public ModelAndView editCarousel(ModelAndView model, @PathVariable("id") Byte id){
        model.setViewName("/carousel/editCarousel");
        model.addObject("carousel",carouselService.getCarousel(id));
        return model;
    }

    /**
     * 编辑轮播图详情
     * @param carousels
     * @return
     */
    @PutMapping("/updateCarousel")
    @ResponseBody
    @LogPoint("编辑轮播图详情")
    @RequiresPermissions("carousel:update")
    public DataResult updateCarousel(Carousels carousels){
        try {
            carouselService.updateCarousel(carousels);
            return DataResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.failed("编辑失败！");
        }

    }
    /**
     * 展示轮播图
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @RequiresPermissions("carousel:select")
    public DataResult list(@RequestParam(value = "page",defaultValue = "1",required = false) Integer currentPage,
                           @RequestParam(value = "limit",defaultValue = "10",required = false) Integer pageSize){
        List<Carousels> carousels = carouselService.findAll(currentPage,pageSize);
        PageInfo info = new PageInfo(carousels);
        return DataResult.success(info);

    }

    /**
     * 上传轮播图片，返回图片路径
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public DataResult upload(MultipartFile file) {
        //转存文件到D：/upload/carousel/img,并返回访问路径
        return uploadConfig.uploadImg(file,loadPath,imgPath+"carousel");
    }

    /**
     * 添加轮播图
     * @param carousels
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    @LogPoint("添加轮播图")
    @RequiresPermissions("carousel:save")
    public DataResult save(Carousels carousels){
        try {
            carouselService.save(carousels);
            return DataResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.failed("添加失败！");
        }

    }
    /**
     * Description : 预览  <br/>
     * ChangeLog : 1. 创建 (2020/3/29 0029 下午 7:46 [Administrator]);
     * @param model
     * @return org.springframework.web.servlet.ModelAndView
    */
    @GetMapping("/preview")
    @LogPoint("预览轮播图")
    @RequiresPermissions("carousel:pre")
    public ModelAndView preview(ModelAndView model){
        List<Carousels> preview = carouselService.findByStatus();
        model.setViewName("/carousel/preview");
        model.addObject("preview",preview);
        return model;
    }
}